package com.songmao.pricecloudapitest.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.testng.ISuite;
import org.testng.Reporter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Remy
 * @description :
 * @date 2020-05-08 20:44
 */
public class TestUitl {

    private static LoggerCore loggerCore = new LoggerCore(TestUitl.class);

    public static Date stringConvertDate(String date, String format) {
        if (date == null) {
            return null;
        }

        if ("".equals(format) || format == null) {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            try {
                return ft.parse(date);
            } catch (ParseException e) {
                return null;
            }
        }
        SimpleDateFormat ft = new SimpleDateFormat(format);
        try {
            return ft.parse(date);
        } catch (ParseException e) {
            return null;
        }


    }


    /**
     * 判断传入时间是否在时间内区间内
     * 如果结束时间为空,那么则判断开始时间是否大于传入时间;如果开始时间小于传入时间则为true,否则false
     * 如果开始时间为空,那么则判断结束时间是否大于传入时间;如果结束时间小于传入时间则为false,否则true
     *
     * @return：boolean
     * @param：startTime
     * @param：endTime
     * @param：intervalTime
     */
    public static boolean judgingTimePeriod(Date startTime, Date endTime, Date intervalTime) {
        if (startTime == null && endTime == null && intervalTime == null) {
            return false;
        }

        if (startTime == null && endTime == null) {
            return false;
        }

        if (intervalTime == null) {
            return false;
        }

        //如果结束时间为空，则判断当前时间和开始时间进行比较
        if (endTime == null) {
            Calendar date = Calendar.getInstance();
            date.setTime(intervalTime);

            Calendar before = Calendar.getInstance();
            before.setTime(startTime);
            return date.after(before);
        }

        if (startTime == null) {
            Calendar date = Calendar.getInstance();
            date.setTime(intervalTime);

            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
            return date.before(end);
        }


        loggerCore.info("startTime:{}", startTime);
        loggerCore.info("endTime:{}", endTime);
        loggerCore.info("intervalTime:{}", intervalTime);
        Calendar date = Calendar.getInstance();
        date.setTime(intervalTime);

        Calendar before = Calendar.getInstance();
        before.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(before) && date.before(end);
    }

    /**
     * 把字符串封装成Map
     *
     * @return:Map
     * @param：str
     */
    public static Map<String, String> splitString(String str) {
        String oneCutter = ",";
        String twoCutter = "=";
        if ("".equals(str) || str == null) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
        String[] oneCutterStr = str.split(oneCutter);
        for (String s : oneCutterStr) {
            String[] i = s.split(twoCutter);
            map.put(i[0], i[1]);
        }
        return map;
    }

    public static int random(int max) {
        Random random = new Random();
        return random.nextInt(max);
    }

    public static String randomString(int length) {
        return RandomStringUtils.randomAlphanumeric(length);
    }

    //替换url参数
    public static String replaceAccessToken(String url, String name, String accessToken) {
        if (url == null) {
            return null;
        }
        return url.replace(name, accessToken);
    }

    public static void setAttribute(String name, String value) {
        ISuite suite = Reporter.getCurrentTestResult().getTestContext().getSuite();
        suite.setAttribute(name, value);
    }

    public static Object getAttribute(String name) {
        ISuite suite = Reporter.getCurrentTestResult().getTestContext().getSuite();
        return suite.getAttribute(name);
    }

    
}
